﻿using NLog;
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;

namespace MT.StartTool
{
    /// <summary>
    /// 编译注释 
    /// 20240627 编译工具，为了启动智能望诊
    /// </summary>
    internal class Program
    {
        private static ILogger log = LogManager.GetCurrentClassLogger();

        static void Main(string[] args)
        {
            if (!CheckIfSpecifiedServiceStart())
            {
                Thread.Sleep(2000);
                Console.ReadLine();
                return;
            }

            log.Info("启动成功！");
            Environment.Exit(0);
        }

        /// <summary>
        /// 检查指定的服务是否启动
        /// </summary>
        public static bool CheckIfSpecifiedServiceStart()
        {
            string serviceName = "MTTongueTeach";
            string start_file = AppDomain.CurrentDomain.BaseDirectory + "mt_start.txt";
            if (!File.Exists(start_file))
            {
                log.Info($"{start_file} 文件不存在");
                return false;
            }

            serviceName = File.ReadAllText(start_file, Encoding.UTF8);

            //检查脉诊是否启动
            bool serviceStarted = false;
            foreach (Process temp in Process.GetProcesses())
            {
                if (temp.ProcessName == serviceName)
                {
                    serviceStarted = true;
                    break;
                }
            }

            if (!serviceStarted)
            {
                string exePath = AppDomain.CurrentDomain.BaseDirectory + $@"{serviceName}.exe";
                if (File.Exists(exePath))
                {
                    Process process = new Process();
                    process.StartInfo.FileName = exePath;
                    process.Start();
                }
                else
                {
                    log.Info($"{serviceName}.exe 文件不存在");
                }
            }

            return serviceStarted;
        }
    }
}
